package com.guodu.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guodu.dao.PurchasingInformationTwoDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.PurchasingInformationTwo;
import com.guodu.entity.PurchasinginfortwoStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.PurchasingInformationTwoService;
import com.guodu.vo.PurchasingInformationTwoQueryVo;
import com.guodu.vo.PurchasingInformationTwoVo;


@Service("purchasingInformationTwoService")
public class PurchasingInformationTwoServiceImpl implements PurchasingInformationTwoService {
	
	
	@Resource
	private PurchasingInformationTwoDao purchasingInformationTwoDao;

	@Resource
	private UnitsDao unitsDao;
	
	@Resource
	private UserInfoDao userInfoDao;
	
	

	@Override
	public long findSizeByVo(PurchasingInformationTwoQueryVo piq, Units units) {
		// TODO Auto-generated method stub
		return purchasingInformationTwoDao.findSizeByVo(piq, units);
	}

	@Override
	public List<PurchasingInformationTwoVo> findByVo(
			PurchasingInformationTwoQueryVo pitq, Units units, int curPage,
			int pageSize) {
		// TODO Auto-generated method stub
		List<PurchasingInformationTwo> list = purchasingInformationTwoDao.findByVo(pitq,units, curPage, pageSize);
		List<PurchasingInformationTwoVo> purchasingInformationTwoVos = new ArrayList<PurchasingInformationTwoVo>();
		for(PurchasingInformationTwo purchasingInformationTwo : list ){
			PurchasingInformationTwoVo purchasingInformationTwoVo = new PurchasingInformationTwoVo(purchasingInformationTwo);
			UserInfo userInfo = userInfoDao.findById(purchasingInformationTwo.getCreatePeople());
			Units unitsInfo = unitsDao.findById(purchasingInformationTwo.getUnitsId());
			purchasingInformationTwoVo.setUnitsName(unitsInfo.getUnitsName());
			purchasingInformationTwoVo.setIndustry(unitsInfo.getIndustry());
			Units createUnits =  unitsDao.findById(userInfo.getUnitsId());
			purchasingInformationTwoVo.setUnitsLevel(createUnits.getUnitsLevel());
			if(unitsInfo.getFunitsId()!=null){
				Units funitsInfo = unitsDao.findById(unitsInfo.getFunitsId());
				purchasingInformationTwoVo.setFunitsName(funitsInfo.getUnitsName());
			}
			purchasingInformationTwoVo.setUserName(userInfo.getUserName());
			purchasingInformationTwoVos.add(purchasingInformationTwoVo);
		}
		return purchasingInformationTwoVos;
	}

	@Override
	public void save(PurchasingInformationTwo purchasingInformationTwo) {
		// TODO Auto-generated method stub
		purchasingInformationTwoDao.save(purchasingInformationTwo);
	}

	@Override
	public PurchasingInformationTwo findById(String purchaseId) {
		// TODO Auto-generated method stub
		return purchasingInformationTwoDao.findById(purchaseId);
	}

	@Override
	public void update(PurchasingInformationTwo purchasingInformationTwo,Long status) {
		// TODO Auto-generated method stub
		purchasingInformationTwoDao.update(purchasingInformationTwo);
		List<PurchasinginfortwoStatus> entities = new ArrayList<PurchasinginfortwoStatus>();
		PurchasinginfortwoStatus entity = new PurchasinginfortwoStatus();
		entity.setAfterStatus(Long.valueOf(0));
		entity.setBeforeStatus(status);
		entity.setCreatePeople(purchasingInformationTwo.getCreatePeople());
		entity.setPurchaseId(purchasingInformationTwo.getPurchaseId());
		entity.setStatusId("1");
		entity.setCreateTime(Calendar.getInstance().getTime());
		entities.add(entity);
		purchasingInformationTwoDao.changeStatus(entities);
	}

	@Override
	public List<PurchasinginfortwoStatus> findStatus(String purchaseId) {
		// TODO Auto-generated method stub
		return purchasingInformationTwoDao.findStatus(purchaseId);
	}

	@Override
	public void examine(String ids, String rs, String beforeStatus,
			Long status, UserInfo user) {
		// TODO Auto-generated method stub
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<PurchasinginfortwoStatus> entities = new ArrayList<PurchasinginfortwoStatus>();
		for(int i=0;i<idses.length;i++){
			PurchasinginfortwoStatus entity = new PurchasinginfortwoStatus();
			entity.setAfterStatus(status);
			entity.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			entity.setCreatePeople(user.getUserId());
			entity.setPurchaseId(idses[i]);
			entity.setReason(rs);
			entity.setStatusId(i+"");
			entity.setCreateTime(Calendar.getInstance().getTime());
			entities.add(entity);
		}
		purchasingInformationTwoDao.changeStatus(entities);
		purchasingInformationTwoDao.examine(ids,status);
	}

	@Override
	public void delByIds(String[] delIds) {
		// TODO Auto-generated method stub
		purchasingInformationTwoDao.delByIds(delIds);
	}

	@Override
	public List<PurchasingInformationTwo> findAllByVo(
			PurchasingInformationTwoQueryVo pitq, Units attribute) {
		// TODO Auto-generated method stub
		return purchasingInformationTwoDao.findAllByVo(pitq,attribute);
	}

	@Override
	public PurchasingInformationTwoVo findAllMoney(
			PurchasingInformationTwoQueryVo pitq, Units units) {
		
		return purchasingInformationTwoDao.findAllMoney(pitq,units);
	}

	@Override
	public void examinAll(PurchasingInformationTwoQueryVo pitq, Units units) {
		// TODO Auto-generated method stub
		purchasingInformationTwoDao.examinAll(pitq,units);
	}

}
